//index.js
//获取应用实例
import { config } from '../../utils/config.js';
import { CouponStoreModel } from '../../model/coupon-store.js';
import { showLoading,  hideLoading } from '../../components/mask.js';
const couponStoreModel = new CouponStoreModel();

Page({
  data: {
	storeGroupId: '',
	storeTitle: '上线城市',
	tipTitle: '点击展开',
	isShowFirstCity: true,
	cityList: [] ,// 加载城市数据
	isShowList: [] ,// 控制展开和关闭
	flagLabels: []
  },
  
  onLoad: function(option) {
	  showLoading();
	  this.setData({
		  storeGroupId: option.storeGroupId
	  });
	  this._loadStoreList(option.storeGroupId);
  },
  
  // 点击展开和关闭
  unFoldPanel: function(e) {
	  let storeIndexId = e.currentTarget.dataset.id;
	  // 获取某一个城市的展开和折叠状态
	  let tempValue = this.data.isShowList[storeIndexId];
	  // 默认关闭
	  if(tempValue) {
		  // 展开
		  this.data.isShowList[storeIndexId] = false;
		  this.data.flagLabels[storeIndexId] = '点击收起';
		  
	  } else {
		  this.data.isShowList[storeIndexId] = true
		  this.data.flagLabels[storeIndexId] = '点击展开';
	  }
	  this.setData({
	  	isShowList: this.data.isShowList,
		flagLabels: this.data.flagLabels
	  });
	  
  },
  
  // 加载适用门店列表
  _loadStoreList: function(storeGroupId) {
	  const couponStoreList = couponStoreModel.getCouponStoreDetail(storeGroupId);
	  couponStoreList.then(res => {
		  if(res && res.data && res.data.cityList) {
			   let tempList = [];
			   let tempShowList = [];
			   let tempOpenLabels = [];
			   for(let item of res.data.cityList) {
				   if(item.geoName) {
					   tempList.push(item);
					   tempShowList.push(true);
					   tempOpenLabels.push('点击展开');
				   }
			   }
			   this.setData({
					cityList: tempList,
					isShowList: tempShowList,
					flagLabels: tempOpenLabels,
			   });
			   hideLoading();
			}
		});
  }
  
});